package org.jeecg.modules.job.rms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.job.rms.entity.RmsOuttask;
import org.jeecg.modules.job.rms.entity.RmsOuttaskApply;
import org.jeecg.modules.job.rms.mapper.RmsOuttaskApplyMapper;
import org.jeecg.modules.job.rms.mapper.RmsOuttaskMapper;
import org.jeecg.modules.job.rms.service.IRmsOuttaskApplyService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 任务报名
 * @Author: jeecg-boot
 * @Date:   2022-12-18
 * @Version: V1.0
 */
@Service
public class RmsOuttaskApplyServiceImpl extends ServiceImpl<RmsOuttaskApplyMapper, RmsOuttaskApply> implements IRmsOuttaskApplyService {

    @Resource
    private RmsOuttaskMapper outtaskMapper;
    @Resource
    private ISysBaseAPI sysBaseAPI;


    @Override
    public boolean addOrUpdate(RmsOuttaskApply param) {
        if (oConvertUtils.isNotEmpty(param.getId())){
            return this.updateById(param);
        }else{
            return this.save(param);
        }
    }

    @Override
    public boolean applyTask(RmsOuttaskApply param) {
        //根据会员id和任务id判断是否已报名
        RmsOuttaskApply result=this.getOne(new QueryWrapper<>(param));
        if (result!=null){
            //修改报名信息
            param.setId(result.getId());
            this.updateById(param);
        }else{
            //更新报名数量
            RmsOuttask outtask=outtaskMapper.selectById(param.getTaskId());
            outtask.setNumber(outtask.getNumber()+1);
            outtaskMapper.updateById(outtask);
            this.save(param);
        }
        return true;
    }

    @Override
    public IPage<RmsOuttaskApply> findPageBySpec(Page<RmsOuttaskApply> page, RmsOuttaskApply param) {
        IPage pageInfo = this.baseMapper.selectPage(page, new QueryWrapper<>(param));
        List<RmsOuttaskApply> list=page.getRecords();
        if(param.getTaskId()==null){//查询用户申请任务
            for (int i=0;i<list.size();i++){
                list.get(i).setOuttask(outtaskMapper.selectOne(new QueryWrapper<>(new RmsOuttask().setId(list.get(i).getTaskId()))));
            }
        }else{//查询任务报名列表
            LoginUser memberResult;
            for (int i=0;i<list.size();i++){
                memberResult=sysBaseAPI.getUserById(list.get(i).getMemberId());
                if (memberResult!=null){
                    list.get(i).setUsername(memberResult.getRealname());
                    list.get(i).setAvatar(memberResult.getAvatar());
                }
            }
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
}
